/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
/* 是否为开发环境 */
export const isDev = process.env.NODE_ENV === "development";

/* 本地缓存操作 */
export const localOperation = {
  get: (key: string) => {
    return localStorage.getItem(key);
  },
  set: ({ key, value }: any) => {
    typeof value === "object" && (value = JSON.stringify(value));
    localStorage.setItem(key, value);
  },
  clear: () => {
    localStorage.clear();
  },
};

/* 过滤换算类型 */
export const filterUnit = (type: number) => {
  switch (type) {
    case 1:
      return "＋";
    case 2:
      return "－";
    case 3:
      return "×";
    case 4:
      return "÷";
    case 5:
      return "÷";
  }
};
/* 核心算法 */
const unit = [10, 100, 1000, 10000, 100000];

const getRonDom = (unitNum: number) =>{
  if(unitNum === 1){
   return Math.floor(
      Math.random() * 8.01 + 2.01
    );
  }
  return Math.floor(
    Math.random() * (unit[unitNum - 1] - unit[unitNum - 1] / 10) +
    unit[unitNum - 1] / 10
  )
};

/**
 * 四舍五入
 * @param num 四舍五入的值
 * @param decimal 要保留的小数位数
 * @returns
 */
export const round = function (num: any, decimal: any) {
  if (isNaN(num)) {
    return 0;
  }
  const p1 = Math.pow(10, decimal + 1);
  const p2 = Math.pow(10, decimal);
  return Math.round(num * p1 / 10) / p2;
};

const deepUnit: any = (
  type: string,
  unitCont: Array<number>,
  unitNum: number,
  isPush = true
) => {
  while (true) {
    const ronDom: any = getRonDom(unitNum);
    const restUnit = () => {
      if (isPush) {
        unitCont.push(ronDom);
      }
      return ronDom;
    };
    if (type === "l" && !unitCont.includes(ronDom)) {
      return restUnit();
    } else {
      return restUnit();
    }
  }
};

/**
 * @function 生成算法
 * @param digitL 左侧位数
 * @param digitR 右侧位数
 * @param time  时间
 * @param current 数量
 * @param type 类型
 */
const subjectCustomStore = JSON.parse(localOperation.get("patchCustom") as any) || JSON.parse(
  localOperation.get("subjectCustom") as any
);
const result = (types: number, l: number, r: number) => {
  switch (types) {
    case 1:
      return l + r;
    case 2:
      return l - r;
    case 3:
      return l * r;
    case 4:
      return l / r;
    case 5:
      return { merchant: Math.floor(l / r), remainder: l % r };
  }
};
export const generateaAlgorithm = ({
  digitL,
  digitR,
  time,
  current,
  type,
}: any) => {
  const repeatRemove = (custom: any) => {
    if (custom && custom.length >= 1) {
      while (true) {
        const dl = deepUnit("l", L, digitL, false);
        const dr = deepUnit("r", R, digitR, false);
        const isRepeat = custom.every((item: any, index: any) => {
          return item.L !== dl && item.R !== dr;
        });
        if (isRepeat) {
          L[i] = dl;
          R[i] = dr;
          break;
        }
      }
    }
  };
  const subjects = [];
  const L: Array<number> = [],
    R: Array<number> = [];
  /* 类型判断 */

  let i = 0;

  while (i < current) {
    /* 生成左侧 */
    deepUnit("l", L, digitL);
    /* 生成右侧 */
    deepUnit("r", R, digitR);
    if (type === 1) {
      repeatRemove(subjectCustomStore && subjectCustomStore[0]);
    }
    if (type === 2) {
      //减法
      if (L[i] < R[i]) {
        while (true) {
          const dl = deepUnit("l", L, digitL, false);
          const dr = deepUnit("r", R, digitR, false);
          if (subjectCustomStore && subjectCustomStore[1] && subjectCustomStore[1].length >= 1) {
            const isRepeat = subjectCustomStore[1].every(
              (item: any, index: any) => {
                return item.L !== dl && item.R !== dr;
              }
            );
            if (isRepeat) {
              if (dl > dr) {
                L[i] = dl;
                R[i] = dr;
                break;
              }
            }
          } else {
            if (dl > dr) {
              L[i] = dl;
              R[i] = dr;
              break;
            }
          }
        }
      }
    }
    if (type === 3) {
      repeatRemove(subjectCustomStore && subjectCustomStore[2] && subjectCustomStore[2]);
    }
    if (type === 4) {
      //整除
      if (L[i] % R[i] !== 0) {
        while (true) {
          const dl = deepUnit("l", L, digitL, false);
          const dr = deepUnit("r", R, digitR, false);
          if (subjectCustomStore && subjectCustomStore[3] && subjectCustomStore[3].length >= 1) {
            const isRepeat = subjectCustomStore[3].every(
              (item: any, index: any) => {
                return item.L !== dl && item.R !== dr;
              }
            );
            if (isRepeat) {
              if (dl % dr === 0) {
                L[i] = dl;
                R[i] = dr;
                break;
              }
            }
          } else {
            if (dl % dr === 0) {
              L[i] = dl;
              R[i] = dr;
              break;
            }
          }
        }
      }
    }
    if (type === 5) {
      //余数除法
      if (L[i] % R[i] == 0) {
        while (true) {
          const dl = deepUnit("l", L, digitL, false);
          const dr = deepUnit("r", R, digitR, false);
          if (subjectCustomStore && subjectCustomStore[4] && subjectCustomStore[4].length >= 1) {
            const isRepeat = subjectCustomStore[4].every(
              (item: any, index: any) => {
                return item.L !== dl && item.R !== dr;
              }
            );
            if (isRepeat) {
              if (dl % dr != 0) {
                L[i] = dl;
                R[i] = dr;
                break;
              }
            }
          } else {
            if (dl % dr != 0) {
              L[i] = dl;
              R[i] = dr;
              break;
            }
          }
        }
      }
    }
    subjects.push({
      L: +L[i],
      R: +R[i],
      time,
      result: result(type, +L[i], +R[i]),
      type,
      value: "",
    });

    i++;
  }

  return subjects;
};

/**
 * @function 题目分配
 */

export const subjectDistribution = (subject: any) => {
  let subjects: any = [];
  for (let i = 0; i < subject.length; i++) {
    subjects = subjects.concat(
      generateaAlgorithm({
        ...subject[i],
      })
    );
  }

  if (subjectCustomStore) {
    for (let i = 0; i < subjectCustomStore.length; i++) {
      for (let j = 0; j < subjectCustomStore[i].length; j++) {
        const item = subjectCustomStore[i][j];
        delete item.id;
        delete item.verification;
        item.result = result(item.type, +item.L, +item.R);
        subjects.push(subjectCustomStore[i][j]);
      }
    }
  }
  /* 洗牌算法 */
  const len = subjects.length;
  for (let i = 0; i < len - 1; i++) {
    const index = Math.floor(Math.random() * (len - i));
    const temp = subjects[index];
    subjects[index] = subjects[len - i - 1];
    subjects[len - i - 1] = temp;
  }
  /* 添加分数 */
  for (let i = 0; i < len; i++) {
    if (subjects[i].type === 5) {
      subjects[i]["remainder"] = "";
    }
    subjects[i]["fraction"] = round((100 / len) , 2);
  }
  console.log(subjects);
  return subjects;
};
